package com.lx.mall.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/**
 * @author lX
 * @version JDK 8
 * @className RibbonConfig (此处以class为例)
 * @date 2024/12/17
 * @description Ribbon负载均衡设置
 */
@Configuration
public class RibbonConfig {

  @Autowired
  private LoadBalancerClient loadBalancer;

  @Bean
  public RestTemplate restTemplate(){
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setInterceptors(
      Collections.singletonList(
        new LoadBalancerInterceptor(loadBalancer)));
    return restTemplate;
  }
}
